import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-05-04
 * Time: 9:22
 */
public class Solution {
    public static int judge(String s) {
        int left = 0;
        int right = s.length() - 1;
        // 从两边开始判断
        while (left < right) {
            // 相同继续往中间走
            if(s.charAt(left) == s.charAt(right)){
                left++;
                right--;
            }else{
                // 不同就只用判断下一个字母与对面的字母是否相同
                // 相同说明就该删除该字母, 否则删除对面的字母
                if(s.charAt(left + 1) == s.charAt(right)){
                    return left;
                }else{
                    return right;
                }
            }
        }
        // 本身就是回文串
        return -1;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for (int i = 0; i < n; i++) {
            String str = in.next();
            System.out.println(judge(str));
        }
    }
}
